package com.zhangxin.aiInterview.manager.work_flow.ai_hr_interviewer;

import cn.hutool.core.util.ObjUtil;
import com.zhangxin.aiInterview.manager.work_flow.ai_hr_interviewer.api.CheckHrChatStatus;
import com.zhangxin.aiInterview.manager.work_flow.ai_hr_interviewer.api.CreateHrConversation;
import com.zhangxin.aiInterview.manager.work_flow.ai_hr_interviewer.api.GetHrChatDetail;
import com.zhangxin.aiInterview.manager.work_flow.ai_hr_interviewer.api.StartHrChat;
import com.zhangxin.aiInterview.model.entity.CozeConversationSession;
import com.zhangxin.aiInterview.model.enums.InterviewTypeEnum;
import com.zhangxin.aiInterview.service.CozeConversationSessionService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 门面设计模式
 * AI Hr面试官智能体接口调用门面类
 */
@Component
public class AiHrInterviewerAgentFacade {

    @Resource
    private CreateHrConversation createHrConversation;

    @Resource
    private StartHrChat startHrChat;

    @Resource
    private CheckHrChatStatus checkHrChatStatus;

    @Resource
    private GetHrChatDetail getHrChatDetail;
    @Resource
    private CozeConversationSessionService cozeConversationSessionService;

    /**
     * 封装完整调用流程：
     * 1. 创建会话
     * 2. 发起对话
     * 3. 轮询直到完成
     * 4. 获取首条回复内容
     * @param userId 用户id
     * @param jsonStr 用户回答所有内容
     * return 当前对话内容
     */
    public String sendAndGetReply(Long userId,String jsonStr) {
        //查询当前用户是否存在可用会话(第一次调用没有会话)
        String conversationId;
        CozeConversationSession conversation = cozeConversationSessionService.lambdaQuery()
                .eq(CozeConversationSession::getUserId, userId)
                .eq(CozeConversationSession::getInterviewType, InterviewTypeEnum.HR_INTERVIEW.getValue())
                .one();

        if(ObjUtil.isEmpty(conversation)) {
            //不存在 智能体会话不存在，则创建会话获取会话id
            conversationId = createHrConversation.create();
            //将会话存入数据库中
            CozeConversationSession cozeConversationSession = new CozeConversationSession();
            cozeConversationSession.setUserId(userId);
            cozeConversationSession.setConversationId(Long.valueOf(conversationId));
            cozeConversationSession.setInterviewType(InterviewTypeEnum.HR_INTERVIEW.getValue());
            cozeConversationSessionService.save(cozeConversationSession);
        }else {
            //存在 就获取当前用户会话id
            conversationId = conversation.getConversationId().toString();
        }
        //1. 让ai面试官提问
        String chatId = startHrChat.start(conversationId,userId,jsonStr);

        //2. 简单轮询等待
        int maxTries = 10;
        int attempts = 0;
        while (attempts++ < maxTries) {
            try {
                Thread.sleep(1000); // 每秒查一次
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return "中断异常";
            }

            if (checkHrChatStatus.isCompleted(conversationId, chatId)) {
                break;
            }
        }
        //3. 返回结果
        String firstContent = getHrChatDetail.getFirstContent(conversationId, chatId, userId);
        System.out.println("ai面试官提问：" + firstContent);
        return firstContent;
    }
}